package com.khaled.protclass.model.prediction;

import java.util.ArrayList;

import com.khaled.protclass.model.base.IFeature;
import com.khaled.protclass.model.base.IProtein;
import com.khaled.protclass.model.bean.prediction.PredictionResult;
import com.khaled.protclass.model.bean.prediction.PredictionForm;
import com.khaled.protclass.model.util.UtilNumber;

public class Prediction
{
	private static Prediction prediction;

	public static Prediction getPrediction()
	{
		if (prediction == null)
		{
			prediction = new Prediction();
		}
		return prediction;
	}

	public ArrayList<PredictionResult> predict(PredictionForm form) throws Exception
	{
		ArrayList<PredictionResult> results = new ArrayList<PredictionResult>();

		// 1. Get the list of protein sequences
		ArrayList<IProtein> proteinList = form.getProteinList();

		// 2. Loop for each protein sequence
		for (IProtein protein : proteinList)
		{
			// 2.1 Get feature vector from the selected features
			StringBuffer featureVector = new StringBuffer();
			for (IFeature feature : form.getFeaturesList())
			{
				featureVector.append(feature.getFeatureVector(protein));
			}

			// 2.2 Classify the feature vector
			Classification classifier = form.getClassifier();
			double predictionScore = classifier.classify(featureVector);

			// 2.3 Check the prediction threshold
			String predictionClass = null;

			if (predictionScore >= form.getSpecificity())
			{
				predictionClass = form.getClass1Text();
			}
			else
			{
				predictionClass = form.getClass2Text();
			}

			// 2.4 Format the results
			results.add(new PredictionResult(protein.getProteinID(), predictionClass, Double.toString(UtilNumber.round(predictionScore, 3))));
		}
		
		return results;
	}
}
